package com.isoft.service.impl;//package com.wuhongjie.service.impl;


import com.isoft.bean.MyMailMessage;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.isoft.bean.UserContext;
import com.isoft.dao.UserDao;
import com.isoft.entity.FlightLine;
import com.isoft.entity.User;
import com.isoft.service.IUserService;
import com.isoft.util.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Slf4j
@Service
public class UserService implements IUserService {

    @Autowired
    UserDao userDao;

    @Autowired
    RedisUtil redisUtil;

    @Autowired
    RedisCache redisCache;

    @Autowired
    MyMailUtil myMailUtil;

    @Autowired
    TemplateEngine templateEngine;


    @Override
    public User getUserLogin(String name, String password) {
        if (null == name || name.trim().length() == 0) {
            UserContext.currentNum = 1;
            UserContext.setCurrentUserType("不明用户");
            UserContext.setDescType("登陆失败-账号为空");
            System.out.println("account is null");
            return null;
        }
        if (null == password || password.trim().length() == 0) {
            UserContext.currentNum = 1;
            UserContext.setCurrentUserType("不明用户");
            UserContext.setDescType("登陆失败-密码为空");
            System.out.println("登陆失败-密码为空");
            return null;
        } else {
            UserContext.currentNum = 1;
            if (userDao.login(name, password) == null)
                UserContext.currentNum = 1;
            UserContext.setCurrentUserType("不明用户");
            UserContext.setDescType("登陆失败-密码错误");
            System.out.println("登陆失败-密码错误");
        }
        return userDao.login(name, password);
    }

    @Override
    public Integer add(User user) {
        if (null == user) {
            return 1;
        }
        if (StringUtil.isEmpty(user.getName())) {
            return 2;
        }
        String UserCode = user.getIdentifyingCode();    //用户输入的验证码
        String code = redisCache.getCacheObject("code");   //正确的验证码，redis缓存中
        if (code == null) {
            //验证码失去有效期
            return 3;
        }
        if (code.equals(UserCode)) {
            //验证码输入正确 注册
            userDao.add(user);
            return 0;
        } else {
            //验证码输入错误
            return 4;
        }
    }

    @Override
    public IPage<User> selectAll(Integer currentPage, Integer size) {
        Page<User> userPage = new Page<>(currentPage, size);
        IPage<User> userIPage = userDao.getAll(userPage);
        return userIPage;
    }

    @Override
    public List<User> selById(Integer id) {
        return userDao.selectByid(id);
    }

    @Override
    public boolean updateById(User user) {
        if (null == user) {
            return false;
        }
        if (!IdUtil.idCheck(user.getId())) {
            return false;
        }
        if (userDao.checkName(user.getName(), user.getId()) == 0) {
            return userDao.updateUserById(user) > 0;
        } else {
            return false;
        }
    }

    @Override
    public boolean updateIdeAndAddressAndPhone(User user) {
        if (null == user) {
            return false;
        }
        if (!IdUtil.idCheck(user.getId())) {
            return false;
        }
        return userDao.updateUserById(user) > 0;
    }

    @Override
    public boolean deleteById(Integer id) {
        return userDao.deleteById(id) > 0;
    }

    @Override
    public boolean updatePhoto(Integer id, String photourl) {
        return userDao.updatePhoto(id, photourl) > 0;
    }

    @Override
    public String getIdentifyingCode(String email) {
        String code = IdentifyingCodeUtil.getCode();
        //存入redis缓存
        redisCache.setCacheObject("code", code);
        redisCache.expire(code, 5, TimeUnit.MINUTES);
        //给用户发送邮件
        Context context = new Context();
        context.setVariable("identifyingCode", code);
        String emailText = templateEngine.process("IdentifyingCode.HTML", context);
        MyMailMessage message = new MyMailMessage();
        System.out.println(email);
        message.setTo(email);
        message.setSubject("您收到了一条验证码");
        message.setText(emailText);
        myMailUtil.sendHtmlMail(message);
        log.info(code);
        return code;
    }

    @Override
    public void updatehead(Integer id, String url) {
        userDao.updatePhoto(id, url);
    }

    @Override
    public boolean updatePwd(Integer id, String password) throws UnsupportedEncodingException, NoSuchAlgorithmException {
        if (password == null) {
            return false;
        }
        if (!IdUtil.idCheck(id)) {
            return false;
        }
        String salt=PwdSaltUtil.getSalt();
        String newPwd=PwdSaltUtil.getEncryptedPwd(password,salt);
        return userDao.updatePwd(id,newPwd,salt) > 0;
    }
}